﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;

public class EginTools
{

    /* ------------ Author: shawn.zp ------------ */

    public static long localBeiJingTime = 0;//北京时间和本地时间差

    /// <summary>
    /// Logs message to the Unity Console.
    /// Author: shawn.zp
    /// </summary>
    public static void Log(string info)
    {
        //Debug.Log("Egin: " + info);
    }


    public static long nowMinis()
    {

        System.DateTime nowDate = System.DateTime.Now;
        System.DateTime d1 = new System.DateTime(1970, 1, 1);
        System.DateTime d2 = nowDate.ToUniversalTime();

        System.TimeSpan ts = new System.TimeSpan(d2.Ticks - d1.Ticks);
        long ms = (long)ts.TotalMilliseconds;   //返回 1970 年 1 月 1 日至今的毫秒数

        return ms;
    }


    public static string encrypTime(string plaintext_data)
    {
        int text_len = plaintext_data.Length;
        if (text_len < 16)
        {
            plaintext_data += "   ";
        }

        byte[] keyBytes = Encoding.UTF8.GetBytes("ysNzMwk7A9jxZakH");
        string kString = "";
        for (int k = 0; k < 16; k++)
        {
            int mth = UnityEngine.Random.Range(0, 10);//Math.random()*10;
            kString += mth;
        }
        byte[] iv = Encoding.UTF8.GetBytes(kString);

        RijndaelManaged rijalg = new RijndaelManaged();
        rijalg.BlockSize = 128;
        rijalg.KeySize = 128;
        rijalg.FeedbackSize = 128;
        rijalg.Padding = PaddingMode.None;
        rijalg.Mode = CipherMode.CBC;
        rijalg.Key = keyBytes;
        rijalg.IV = iv;

        ICryptoTransform encryptor = rijalg.CreateEncryptor(rijalg.Key, rijalg.IV);

        byte[] plaintextA = new byte[16];//Encoding.UTF8.GetBytes(plaintext_data);//new byte[plaintext_data.Length](plaintext_data);

        //Debug.Log (plaintext_data[0]+":=:"+plaintext_data[13]+"plaintextA:"+plaintext_data.Length);

        for (int k1 = 0; k1 < 16; k1++)
        {
            plaintextA[k1] = (byte)(plaintext_data[k1]);
        }



        byte[] src;

        using (System.IO.MemoryStream msEncrypt = new System.IO.MemoryStream())
        {
            using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
            {
                //Write all data to the stream.
                csEncrypt.Write(plaintextA, 0, plaintextA.Length);
                csEncrypt.FlushFinalBlock();

                src = msEncrypt.ToArray();
            }
        }

        //byte[] src = decryptor.TransformFinalBlock(plaintextA, 0, plaintextA.Length);

        int ivLeng = iv.Length;
        int bleng = ivLeng + src.Length;
        byte[] reByteArr = new byte[bleng];
        for (int k = 0; k < bleng; k++)
        {
            if (k < ivLeng)
            {
                reByteArr[k] = iv[k];
            }
            else
            {
                reByteArr[k] = src[k - ivLeng];
            }
        }

        string restr = System.Convert.ToBase64String(reByteArr);
        return restr;
    }

    /// <summary>
    /// Clear all children in [Transform].
    /// Author: shawn.zp
    /// </summary>
    public static void ClearChildren(Transform transform)
    {
        foreach (Transform child in transform)
        {
            Object.Destroy(child.gameObject);
        }
    }

    /// <summary>
    /// Return MD5 coding.
    /// Author: shawn.zp
    /// </summary>
    public static string MD5Coding(string input)
    {
        System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
        byte[] bytes = ue.GetBytes(input);

        // encrypt bytes
        System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
        byte[] hashBytes = md5.ComputeHash(bytes);

        // Convert the encrypted bytes back to a string (base 16)
        string hashString = "";
        for (int i = 0; i < hashBytes.Length; i++)
        {
            hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
        }

        return hashString.PadLeft(32, '0');
    }


}